import time

from fastapi_pagination.bases import AbstractPage
from fastapi_pagination.ext.databases import paginate
from sqlalchemy import select

from forward.api.admin.schemas.system import (
    SystemLogOperateInSchema,
    SystemLogLoginInSchema,
    SystemLogOperateOutSchema,
    SystemLogLoginOutSchema,
)
from forward.dependencies.database import db
from forward.model.tables import SystemLogLoginModel, SystemLogOperateModel, UserAuthModel
from forward.model.tables.system import SystemAuthAdminModel


class SystemLogsServer:
    """系统日志服务实现类"""

    async def operate(
        self, operate_in: SystemLogOperateInSchema
    ) -> AbstractPage[SystemLogOperateOutSchema]:
        """系统操作日志"""

        # 查询条件
        where = []
        if operate_in.title:
            where.append(SystemLogOperateModel.title.like(f"%{operate_in.title}%"))
        if operate_in.username:
            where.append(
                SystemAuthAdminModel.username.like(f"%{operate_in.username}%")
            )
        if operate_in.ip:
            where.append(SystemLogOperateModel.ip.like(f"%{operate_in.ip}%"))
        if operate_in.type:
            where.append(SystemLogOperateModel.type == operate_in.type)
        if operate_in.status is not None:
            where.append(SystemLogOperateModel.status == operate_in.status)
        if operate_in.url:
            where.append(SystemLogOperateModel.url == operate_in.url)
        if operate_in.start_time:
            where.append(
                SystemLogOperateModel.create_time
                >= operate_in.start_time
            )
        if operate_in.end_time:
            where.append(
                SystemLogOperateModel.create_time
                <= operate_in.end_time
            )
        query = (
            select(SystemLogOperateModel,SystemAuthAdminModel.username,SystemAuthAdminModel.nickname)
            .where(*where)
            .outerjoin(
                SystemAuthAdminModel,
                SystemLogOperateModel.admin_id == SystemAuthAdminModel.id,
            )
            .order_by(SystemLogOperateModel.id.desc())
        )
        pager = await paginate(db, query)
        return pager

    async def login(
        self, login_in: SystemLogLoginInSchema
    ) -> AbstractPage[SystemLogLoginOutSchema]:
        """系统登录日志"""
        # 查询条件
        where = []
        if login_in.username:
            where.append(SystemLogLoginModel.username.like(f"%{login_in.username}%"))
        if login_in.status:
            where.append(SystemLogLoginModel.status == login_in.status)
        if login_in.start_time:
            where.append(
                SystemLogLoginModel.create_time
                >= login_in.start_time
            )
        if login_in.end_time:
            where.append(
                SystemLogLoginModel.create_time
                <= login_in.end_time
            )
        query = (
            select(SystemLogLoginModel)
            .where(*where)
            .order_by(SystemLogLoginModel.id.desc())
        )
        pager = await paginate(db, query)
        return pager

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
